<?php
/**
 * Project:
 * File:        Strings.php
 *
 * @link 		http://.../lib
 * @copyright 	2007
 * @author 		ivan <ldmmyx@hotmail.com>
 * @version 	1.0
 * 
 * @ substr_cut($str_cut,$length = 65)
 */
class StringUtils
{
	

	public static function getHost($URL)
	{
		$URL = str_ireplace('http:///', '', $URL);
		
		$arr = parse_url($URL, PHP_URL_HOST);
		
		return $arr;
	}
	
	public static function filterTopDomain($url)
	{
		$topLevelDomain = array ('.com', '.edu', '.gov', '.int', '.mil', '.net', '.org', '.biz', '.info', '.pro', '.name', '' .
				'.museum', '.coop', '.aero', '.xxx', '.idv', '.mobi', '.cc', '.me' );
		
		return str_replace($topLevelDomain, '', $url);
	}
	
	public static function getTopDomain($URL)
	{
		preg_match("/[\w\-]+\.\w+(?=\/)/", $URL, $arr);

		return $arr[0];
	}
	
	public static function getNextLevelDomain($URL)
	{
		$reg = "#(?<=\\.)([\\w\-]+\\.)+?\\w+(?=/)#ix";
		
		preg_match($reg, $URL, $arr);

		return $arr[0];
	}
	
	public static function count_words($string) 
	{ 
	    // split text by ' ',\r,\n,\f,\t 
	    $split_array = preg_split('/\s+/',$string); 
	    // count matches that contain alphanumerics 
	    $word_count = preg_grep('/[a-zA-Z0-9\\x80-\\xff]/', $split_array); 
	
	    return count($word_count); 
	}
	
	public static function getCharNum($text)
	{
		return StringUtils::getCharNumReplace($text);
	}

	public static function getCharNumReplace($text, $replace = '-')
	{
		$text	= trim($text);
		
		$return = '';
		$len 	= strlen($text);
		for($i = 0; $i < $len; $i++){
			$ch = ord($text[$i]);
			if($ch > 47 && $ch < 58 || $ch > 64 && $ch < 91 || $ch > 96 && $ch < 123)
				$return .= $text[$i];
			else{
				$return .= $replace;
			}
		}
		
		$return	= StringUtils::deleteExcessChars($return, $replace . $replace, $replace);
		
		return trim($return, '-');
	}
	
	public static function isUpperNum($text)
	{
		$text	= trim($text);
		
		$len 	= strlen($text);
		for($i = 0; $i < $len; $i++){
			$ch = ord($text[$i]);
			if($ch > 47 && $ch < 58 || $ch > 64 && $ch < 91)
				continue;
			else{
				return false;
			}
		}
		
		return true;
	}
	
	public static function getChars($text)
	{
		$text	= trim($text);
		
		$len 	= strlen($text);
		for($i = 0; $i < $len; $i++){
			$ch = ord($text[$i]);
			if($ch > 64 && $ch < 91 || $ch > 96 && $ch < 123)
				continue;
			else{
				return false;
			}
		}
		
		return true;
	}
	
	public static function isCouponCode($text)
	{
		$text	= trim($text);
		
		$len 	= strlen($text);
		for($i = 0; $i < $len; $i++){
			$ch = ord($text[$i]);
			if($ch > 47 && $ch < 58 || $ch > 64 && $ch < 91 || $ch > 96 && $ch < 123 || $ch == 45 || $ch == 95)
				continue;
			else{
				return false;
			}
		}
		
		return true;
	}
	
	public static function deleteExcessChars($text, $search, $replace)
	{
		$pos = strrpos($text, $search);
		
		if ($pos === false) { // note: three equal signs
		    return $text;
		}else{
			while(true){
				$pos = strrpos($text, $search);
				if ($pos === false) { // note: three equal signs
					break;
				}else{
					$text = str_replace($search, $replace, $text);					
				}
			}
		}

		return $text;
	}
	
	public static function deleteExcessSpaces($text)
	{
		return StringUtils::deleteExcessChars($text, '  ', ' ');
	}
	
	public static function getCutValue($startFlag, $endFlag, $content)
	{
		$value 	= '';
		
		$lowerContent = strtolower($content);
		$pos1 = strpos($lowerContent, $startFlag);
	
	    if( $pos1 !== false ) {
	    	$startLen = strlen($startFlag);
	    	
	    	$pos2 = strpos($lowerContent, $endFlag, $pos1 + $startLen);
	    	if( $pos2 !== false ) {
	   			$value = substr($content, $pos1 + $startLen, $pos2 - $pos1 - $startLen);
	    	}
	    }
	
		return $value;
	}

	public static function getStartValue($endFlag, $content)
	{
		$value 	= $content;
		
		$lowerContent = strtolower($content);
		$pos1 = strpos($lowerContent, $endFlag);
	
	    if( $pos1 !== false ) {
	   		$value = substr($content, 0, $pos1);
 	    }
	
		return $value;
	}
	
	/**
	 *  filter image URL
	 *  getImgUrl
	 *  $str search string
	 *  $num get $num URL or image
	 *  @return String
	 * */
	function getImgUrl($str,$num = 1)
	{
	    $reg = '/<img(.*?)src=(.*?)\/?>/i';
	    preg_match_all($reg,$str,$imgList);
	
	     if(!empty($imgList))
	     {
	        if(isset($imgList[0][$num-1]))
	        {
	            $img = $imgList[0][$num-1];
	            preg_match('/src=(.*?)\.\w{3,4}/i',$img,$url);
	            $rep = array('src="',"src='",'src=');
	            if(!empty($url))
	                return str_replace($rep,'',$url[0]);
	            else
	                return '';
	        }
	        else
	            return '';
	     }
	     else
	        return '';
	}
	
}
?>
